from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI


#widget code
class raw2comp(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        #python variable
        self.data = {}
        self.RFunctionParam_X = ''
        
        #Unique R variable
        self.setRvariableNames(['closed'])
        
        #Inputs
        self.inputs.addInput("X", "X", signals.base.RDataFrame, self.processX)
        
        #Outputs
        self.outputs.addOutput("closed Output","closed", signals.base.RMatrix)#, Racomp)
        
        #GUI
        area = redRGUI.base.widgetBox(self.controlArea,orientation='vertical')  
        self.sizeSignifiance = redRGUI.base.radioButtons(area,  label = "Size signifiance", 
        buttons = ['Yes','No'], setChecked='No',orientation='horizontal', callback = self.onTestChange)
        self.naturalScale = redRGUI.base.radioButtons(area,  label = "Natural scale", 
        buttons = [('real','Difference (real)'),('log','Quotient (log)')], setChecked='log',orientation='horizontal', callback = self.onTestChange)
        self.naturalScale.setEnabled(True)
        self.method = redRGUI.base.lineEdit(area, label = "Method:", text = 'acomp')
        self.method.setDisabled(True)
        self.total = redRGUI.base.lineEdit(self.controlArea, label = "total:", text = '1')
        
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignRight, 
        callback = self.commitFunction, processOnInput=True)

    def onTestChange(self):
        if self.sizeSignifiance.getChecked() =='Yes' and self.naturalScale.getChecked() =='Difference (real)' :
            self.method.setText('rplus')
            self.total.setDisabled(True)
        elif self.sizeSignifiance.getChecked() =='Yes' and self.naturalScale.getChecked() =='Quotient (log)' :
            self.method.setText('aplus')
            self.total.setDisabled(True)
        elif self.sizeSignifiance.getChecked() =='No' and self.naturalScale.getChecked() =='Difference (real)' :
            self.method.setText('rcomp')
            self.total.setEnabled(True)
        elif self.sizeSignifiance.getChecked() =='No' and self.naturalScale.getChecked() =='Quotient (log)' :
            self.method.setText('acomp')
            self.total.setEnabled(True)
            
    def processX(self, data):
        if not self.require_librarys(["compositions"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_X=data.getData()
            self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_X=''

    def commitFunction(self):
        if str(self.RFunctionParam_X) == '':
            self.status.setText('Data is missing.')
            return
        
        injection = []
        if str(self.total.text()) != '' and self.method.text() in ['rcomp','acomp'] :
            string = 'total='+str(self.total.text())+''
            injection.append(string)
        inj = ','.join(injection)
        
        self.R(self.Rvariables['closed']+'<-'+self.method.text()+'(X='+str(self.RFunctionParam_X)+','+inj+')', processingNotice=True)
        newData = signals.base.RMatrix(self, data = self.Rvariables['closed'], checkVal = False) 
        self.rSend("closed Output", newData)
